import { defineStore } from "pinia";
import { ref } from "vue";
import { getTolPriceAPI, paySuccessfulAPI } from '@/apis/pay'
import { createOrderAPI, getOrderDetailsAPI, previewOrderAPI } from "@/apis/order";
import { ElMessage } from "element-plus";
export const usePayStore = defineStore('pay', () => {
    //支付的订单的详情,{state,tolPrice,discount,payment,time,,goodList}
    const details = ref(null)
    const getOrderDetails = async (oid) => {
        const res = await getOrderDetailsAPI(oid);
        details.value = res.data
    }

    const getOrderCache = async (selectionList) => {
        const res = await previewOrderAPI(selectionList)
        const item = res.data;
        details.value = {
            state: 0,
            tolPrice: item.tolPrice,
            discount: item.discount,
            payment: item.payment,
            time: null,
            goodList: selectionList
        }

    }

    const lastPage = ref(0)
    const setLastPage = (pageId) => {
        lastPage.value = pageId
    }
    const paySuccessful = async () => {
        try {
            await paySuccessfulAPI(details.value.oid);
            details.value.state = 3;
            ElMessage({ type: 'success', message: '支付成功' })
        } catch {

        }
    }
    return {
        details,
        lastPage,
        setLastPage,
        getOrderDetails,
        getOrderCache,
        paySuccessful
    }


}, {
    persist: true
})